package com.manster.security;

import com.manster.entity.SysUser;
import com.manster.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author manster
 * @Date 2022/3/15
 **/
@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    SysUserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        SysUser sysUser = userService.getByUsername(username);

        if(sysUser == null){
            throw new UsernameNotFoundException("用户名或密码错误");
        }

        return new AccountUser(sysUser.getId(), sysUser.getUsername(), sysUser.getPassword(), getUserAuthority(sysUser.getId()));
    }

    //获取用户权限信息(角色，菜单)
    public List<GrantedAuthority> getUserAuthority(Long userId){

        //角色(ROLE_admin)，菜单权限(sys:user:list)
        String authority = userService.getUserAuthorityInfo(userId);
        //ROLE_admin,ROLE_normal,sys:user:list,...
        return AuthorityUtils.commaSeparatedStringToAuthorityList(authority);
    }

}
